package com.yiwei.hdb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.base.ItemAdapter;
import com.yiwei.hdb.model.AlipayListModel;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.util.ToastUtil;
import com.lljjcoder.view.SwipeDragLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/17 0017.
 */

public class AlipayUserAdapter extends ItemAdapter<AlipayListModel.DataBean, AlipayUserAdapter.AlipayUserViewHolder> {


    private Context context;
    private boolean isEdit = false;
    private String alipayUser;
    public int selectPos = -1;
    public boolean isClick = false;

    public AlipayUserAdapter(Context context, String alipayUser) {
        super(context);
        this.context = context;
        this.alipayUser = alipayUser;
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();
    }

    @Override
    public AlipayUserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AlipayUserViewHolder(inflate(R.layout.item_add_user, parent));
    }

    @Override
    public void onBindViewHolder(AlipayUserViewHolder holder, int position) {
        AlipayListModel.DataBean item = getItem(position);
        holder.txtUser.setText(item.account);
        if (item.account.equals(alipayUser) && !isClick) {
            holder.cbDikou.setImageResource(R.mipmap.img_pay_cb_open);
        } else {
            if(selectPos==position){
                holder.cbDikou.setImageResource(R.mipmap.img_pay_cb_open);
            }else {
                holder.cbDikou.setImageResource(0);
            }

        }
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAlipayClick != null) {
                    onAlipayClick.onClick(item.account, position);
                }
            }
        });
        holder.trash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAlipayClick != null) {
                    onAlipayClick.delete(item.account);
                }
            }
        });
        if (isEdit) {
            holder.trash.setVisibility(View.VISIBLE);
        } else {
            holder.trash.setVisibility(View.GONE);
        }
    }

    public interface OnAlipayClick {
        void onClick(String account, int poi);

        void delete(String account);
    }

    private OnAlipayClick onAlipayClick;

    public void setonAlipayClickListener(OnAlipayClick onAlipayClick) {
        this.onAlipayClick = onAlipayClick;
    }

    class AlipayUserViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_user)
        TextView txtUser;
        @BindView(R.id.cb_dikou)
        ImageView cbDikou;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        //        @BindView(R.id.star)
//        ImageView star;
        @BindView(R.id.trash)
        TextView trash;
//        @BindView(R.id.swip_layout)
//        SwipeDragLayout swipLayout;

        private AlipayUserViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }

    @Override
    public void update(List<AlipayListModel.DataBean> dataBeen) {
        super.update(dataBeen);
    }
}
